package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.LogisticsCompanies;
import com.example.mall.dto.LogisticsCompaniesDto;
import com.example.mall.mapper.LogisticsCompaniesMapper;
import com.example.mall.query.LogisticsCompaniesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ILogisticsCompaniesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物流公司表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class LogisticsCompaniesServiceImpl extends ServiceImpl<LogisticsCompaniesMapper, LogisticsCompanies> implements ILogisticsCompaniesService {

    @Autowired
    private LogisticsCompaniesMapper logisticsCompaniesMapper;

    @Override
    public PageDTO<LogisticsCompaniesDto> queryPage(LogisticsCompaniesQuery logisticsCompaniesQuery) {
        if (logisticsCompaniesQuery == null) {
            logisticsCompaniesQuery = new LogisticsCompaniesQuery();
        }
        Page<LogisticsCompanies> page = logisticsCompaniesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<LogisticsCompanies> cartPage = lambdaQuery()
                .like(logisticsCompaniesQuery.getCampanyId()!= null, LogisticsCompanies::getCampanyId, logisticsCompaniesQuery.getCampanyId())
                .like(logisticsCompaniesQuery.getName()!= null, LogisticsCompanies::getName, logisticsCompaniesQuery.getName())
                .like(logisticsCompaniesQuery.getDescription()!= null, LogisticsCompanies::getDescription, logisticsCompaniesQuery.getDescription())
                .like(logisticsCompaniesQuery.getIsActive()!= null, LogisticsCompanies::getIsActive, logisticsCompaniesQuery.getIsActive())
                .orderByDesc(LogisticsCompanies::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, LogisticsCompaniesDto.class);
    }

    @Override
    public LogisticsCompanies queryLogisticsCompaniesById(Long campanyId) {
        AssertUtil.isNotNull(campanyId, "物流公司ID不能为空");
        return logisticsCompaniesMapper.selectById(campanyId);
    }

    @Override
    public List<LogisticsCompanies> queryLogisticsCompaniesList() {
        return this.list();
    }

    @Override
    public void insert(LogisticsCompaniesDto logisticsCompaniesDto) {
        LogisticsCompanies logisticsCompanies = new LogisticsCompanies();
        CopyUtils.copyProperties(logisticsCompaniesDto, logisticsCompanies);
        logisticsCompanies.setCampanyId(CodeGenerateUtils.generateUniqueId());
        logisticsCompanies.setCreateDate(new Date());
        logisticsCompanies.setUpdateDate(new Date());
        logisticsCompanies.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        logisticsCompaniesMapper.insert(logisticsCompanies);
    }

    @Override
    public void update(LogisticsCompaniesDto logisticsCompaniesDto) {
        AssertUtil.isNotNull(logisticsCompaniesDto.getCampanyId(), "物流公司ID不能为空");
        LogisticsCompanies logisticsCompanies = logisticsCompaniesMapper.selectById(logisticsCompaniesDto.getCampanyId());
        AssertUtil.isNotNull(logisticsCompanies, "物流公司不存在");
        CopyUtils.copyProperties(logisticsCompaniesDto, logisticsCompanies);
        logisticsCompanies.setUpdateDate(new Date());
        logisticsCompaniesMapper.updateById(logisticsCompanies);
    }

    @Override
    public void delete(Long campanyId) {
        AssertUtil.isNotNull(campanyId, "物流公司ID不能为空");
        LogisticsCompanies logisticsCompanies = logisticsCompaniesMapper.selectById(campanyId);
        AssertUtil.isNotNull(logisticsCompanies, "物流公司不存在");
        logisticsCompanies.setIsActive(BaseConstants.LOSE_STATUS);
        logisticsCompaniesMapper.updateById(logisticsCompanies);
    }

    @Override
    public void deleteBatch(List<Long> campanyIds) {
        AssertUtil.isNotNull(campanyIds, "物流公司ID列表不能为空");
        List<LogisticsCompanies> logisticsCompanies = logisticsCompaniesMapper.selectBatchIds(campanyIds);
        AssertUtil.isNotNull(logisticsCompanies, "物流公司不存在");
        for (LogisticsCompanies logisticsCompany : logisticsCompanies) {
            logisticsCompany.setIsActive(BaseConstants.LOSE_STATUS);
            logisticsCompany.setUpdateDate(new Date());
            logisticsCompaniesMapper.updateById(logisticsCompany);
        }
    }
}
